import { defineStore } from 'pinia'
import { DocumentApi } from "@/api/document/index.js";
import { ElMessage } from "element-plus";

// 定义文档管理的Pinia store
export const useDocumentStore = defineStore('document', {
    state: () => ({
        documentList: [], // 文档列表数据
        documentDetail: null, // 当前查看的文档详情
        relatedLaws: [], // 相关法律条款
        templateList: [], // 模板列表
        signatureData: null, // 签名数据

        // 分页信息
        pageInfo: {
            page: 1,
            size: 10,
            total: 0,
            totalPage: 0
        },

        // 查询条件
        queryParams: {
            caseId: '',
            title: '',
            status: '',
            startTime: '',
            endTime: ''
        },

        // 导出参数
        exportParams: {
            format: 'pdf' // pdf/word
        }
    }),

    actions: {
        // 获取文档分页列表
        async getDocumentList() {
            // 构建查询参数对象
            const params = {
                ...this.queryParams,
                page: this.pageInfo.page,
                size: this.pageInfo.size
            };

            try {
                const response = await DocumentApi.list(params);
                this.documentList = response.data;
                console.log("response",response);
                console.log("response.data",response.data);
                console.log("this.documentList",this.documentList);


                // 更新分页信息
                this.pageInfo.page = response.data.current;
                this.pageInfo.size = response.data.size;
                this.pageInfo.total = response.data.total;
                this.pageInfo.totalPage = response.data.pages;

                return response;
            } catch (error) {
                ElMessage.error('获取文档列表失败');
                throw error;
            }
        },

        // 生成新文档
        async generateDocument(caseId) {
            try {
                const response = await DocumentApi.generate(caseId);
                ElMessage.success('文档生成成功');
                this.getDocumentList(); // 刷新列表
                return response;
            } catch (error) {
                ElMessage.error('文档生成失败');
                throw error;
            }
        },

        // 获取文档详情
        async getDocumentDetail(id) {
            try {
                const response = await DocumentApi.detail(id);
                this.documentDetail = response.data;
                console.log("response.data", response.data);
                console.log("documentDetail", this.documentDetail);

                return response;
            } catch (error) {
                ElMessage.error('获取文档详情失败');
                throw error;
            }
        },

        // 获取相关法律条款
        async getRelatedLaws(docId) {
            try {
                const response = await DocumentApi.laws(docId);
                console.log("relatedLa11111111111111111111111111ws");
                this.relatedLaws = response.data;
                console.log("relatedLaws",this.relatedLaws);
                return response;
            } catch (error) {
                ElMessage.error('获取法律条款失败');
                throw error;
            }
        },

        // 导出文档
        async exportDocument(docId, format) {
            try {
                const response = await DocumentApi.export(docId, format);
                // 处理导出响应
                if (response.data) {
                    window.open(response.data, '_blank');
                }
                return response;
            } catch (error) {
                ElMessage.error('文档导出失败');
                throw error;
            }
        },

        // 签名文档
        async signDocument(docId, signImage) {
            try {
                const response = await DocumentApi.sign(docId, signImage);
                this.signatureData = response.data;
                ElMessage.success('文档签名成功');
                this.getDocumentList(); // 刷新列表
                return response;
            } catch (error) {
                ElMessage.error('文档签名失败');
                throw error;
            }
        },

        // 获取模板列表
        async getTemplates() {
            try {
                const response = await DocumentApi.templates();
                this.templateList = response.data;
                return response;
            } catch (error) {
                ElMessage.error('获取模板列表失败');
                throw error;
            }
        },

        // 添加模板
        async addTemplate(template) {
            try {
                const response = await DocumentApi.addTemplate(template);
                ElMessage.success('模板添加成功');
                this.getTemplates(); // 刷新模板列表
                return response;
            } catch (error) {
                ElMessage.error('模板添加失败');
                throw error;
            }
        },

        // 重置查询条件
        resetQueryParams() {
            this.queryParams = {
                caseId: '',
                title: '',
                status: '',
                startTime: '',
                endTime: ''
            };
        },

        // 处理分页变化
        handlePageChange(page) {
            this.pageInfo.page = page;
            this.getDocumentList();
        }
    }
});